package org.fujene.communicate.hash;

/**
 * It generates hash.
 * 
 * @author fujisaka_yusuke
 */
public final class Hashing{
	private static int division;
	
	public static void setDivision(int div) {
		division = div;
	}
	
	public static long getTermHash(String str){
		long val = str.hashCode();
		val *= 775511;
		val ^= (val << 32);
		val += 0x8CFCD4A7E2D1F043L;
		val ^= (val << 3);
		val += (val >> 37);
		val ^= (val >> 1);
		val ^= (val << 7);
		
		return val;
	}

	public static long getContentHash(long docID){
		docID *= 7777;
		docID ^= docID << 32;
		docID += 0xD3B7FFE45ABD4C7L;
		docID ^= docID << 3;
		docID ^= docID >> 37;
		docID ^= docID >> 1;
		docID ^= docID << 7;
		docID ^= docID << 43;
		
		return docID;
	}
	
	public static long getNodeHash(String IPAddr, int port, int index) {
		if(index >= division) throw new IndexOutOfBoundsException();
		long val = IPAddr.hashCode() * port;
		val *= 7799;
		val ^= val << 32;
		val += 0xD3B7FFE45ABD4C7L;
		val ^= val << 3;
		val ^= val >> 37;
		val ^= val >> 1;
		val ^= val << 7;
		val ^= val << 43;
		
		return val;
	}
}
